'use strict'

const mongoose = require('mongoose')
const Schema = mongoose.Schema
// const ObjectId  = Schema.ObjectId
const _const = require('../commons/const')

/**
 * 用户 (后台管理系统使用)
 */
const adminSchema = new Schema({
    name: {type: String, require:false},
    username: {
        type: String,
        required: true
    }, //登录名(邮箱)
    password: {
        type: String,
        required: true,
        minlength: 6
    }, //密码
    avatar: {
        type: String,
        default: _const.ADMIN_USER_ROLE.avatar
    }, //avatar
    disable: {
        type: Boolean,
        default: false
    }, //是否禁用
    role_type: {
        type: String,
        default: _const.ADMIN_USER_ROLE.ROOT_ADMIN_USER
    }, //所属的角色 0=ROOT_ADMIN_USER 1-ROOT_WAREHOUSE_USER  2-ROOT_OPERATION_USER : 接口传参可传0-1-2
    role: { type: Schema.Types.ObjectId, ref: 'role', required: false}, //角色
    is_super: {type: Number,enum: [1,2]},   //是否为超级管理员 1:否 2:是
    desc: {
        type: String
    }, // 备注
    login_time: {
        type: Date,
        default: Date.now
    }, // 登录时间

    enterprise_id: { type: Schema.Types.ObjectId, ref: 'enterprise', required: false}, //关联企业

    status: {type: Number, enum: [1,2], default: 1},  //状态 1:正常 2:删除

    create_at: {
        type: Date,
        default: Date.now
    },
    update_at: {
        type: Date,
        default: Date.now
    },

})


adminSchema.index({
    username: 1
}, {
    unique: true
}) // 性能要求高的时候 手动加索引

adminSchema.pre('save', function (next) {
    let now = new Date()
    this.update_at = now
    next()
})
mongoose.model('admin', adminSchema)

module.exports = adminSchema
